// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class Flower extends cc.Component {
  type: string = "";
  // LIFE-CYCLE CALLBACKS:
  mainNode: cc.Node = null;

  originalSpriteFrame: cc.SpriteFrame = null;
  touchedSpriteFrame: cc.SpriteFrame = null;

  // 是否能触摸
  private _canTouch: boolean = true;
  set canTouch(boo: boolean) {
    this._canTouch = boo;
  }
  get canTouch() {
    return this._canTouch;
  }
  // 第几个花盆
  private _pengNo: number = 0;
  set pengNo(num: number) {
    this._pengNo = num;
  }
  get pengNo() {
    return this._pengNo;
  }
  // 第几层
  private _floor: number = 0;
  set floor(num: number) {
    this._floor = num;
  }
  get floor() {
    return this._floor;
  }

  // orginWorldPos: cc.Vec2 = null;
  // orginParent: cc.Node = null;

  private _pengPotNode: cc.Node = null; //原来的pengziPos节点
  set pengPotNode(node: cc.Node) {
    this._pengPotNode = node;
  }
  get pengPotNode() {
    return this._pengPotNode;
  }

  onLoad() {
    this.type = this.node.name;
    this.originalSpriteFrame = this.node.getComponent(cc.Sprite).spriteFrame;
    this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this, true);
    this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this, true);
    this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this, true);
  }

  start() {
    this.mainNode = cc.find("Canvas/mainNode");
    // _0204_1
    // 动态加载触摸状态的精灵帧
    const spritePath = `Blossom Sort/flowers-描边/_0${
      205 - Number(this.node.name)
    }_${this.node.name}`; // 根据节点名称构建资源路径
    cc.resources.load(spritePath, cc.SpriteFrame, (err, spriteFrame) => {
      if (err) {
        cc.error(`加载触摸状态图片失败: ${err}`);
        return;
      }
      this.touchedSpriteFrame = spriteFrame;
    });
    // this.orginWorldPos = this.node.parent
    //   .convertToWorldSpaceAR(this.node.getPosition())
    //   .clone();
    // this.orginParent = this.node.parent;
  }

  onTouchStart(event: cc.Event.EventTouch) {
    console.log(
      this._canTouch,
      "---",
      this._floor,
      "----",
      this.pengPotNode.parent.name
    );
    if (!this._canTouch) return;
    // this.orginParent = this.node.parent;
    cc.log("---", this.pengPotNode);
    console.log("touchstart", this.node.parent);
    const sprite = this.node.getComponent(cc.Sprite);
    if (sprite && this.touchedSpriteFrame) {
      sprite.spriteFrame = this.touchedSpriteFrame;
    }
    // this.orginWorldPos = this.node.parent
    //   .convertToWorldSpaceAR(this.node.getPosition())
    //   .clone();
    let touchPos = this.node.getPosition();
    let touchPosWorld = this.node.parent.convertToWorldSpaceAR(touchPos);
    this.node.setParent(this.mainNode);
    let localPos = this.mainNode.convertToNodeSpaceAR(touchPosWorld);
    this.node.setPosition(localPos);
    this.node.setRotation(0);
  }
  onTouchMove(event: cc.Event.EventTouch) {
    if (!this._canTouch) return;
    let touchPos = this.node.getPosition();
    // let touchPosWorld = this.node.parent.convertToWorldSpaceAR(touchPos);
    this.node.setPosition(
      new cc.Vec3(
        touchPos.x + event.getDeltaX(),
        touchPos.y + event.getDeltaY(),
        cc.Vec3.ZERO.z
      )
    );
  }
  onTouchEnd(event: cc.Event.EventTouch) {
    if (!this._canTouch) return;
    const sprite = this.node.getComponent(cc.Sprite);
    if (sprite && this.originalSpriteFrame) {
      sprite.spriteFrame = this.originalSpriteFrame;
    }
    // cc.director.emit("touchEnd", this.node, new Node(), this.orginWorldPos);
    cc.director.emit("touchEnd", this.node);
  }

  // update (dt) {}
}
